<?php

/* $start_time=microtime(true);
 for($i=0;$i<2;$i++){

        $process= new swoole\Process(function ($worker) use($start_time){
              sleep(1);
              $end_time=microtime(true);
              echo $end_time-$start_time;
        });
        $pid=$process->start(); //开启子进程

 }
*/

$data=[
    '990979940@qq.com',
    '213123@qq.com',
    '457567657@qq.com'
];


//主进程
$start_time=microtime(true);
//发邮件失败返回给主进程
$workers=[];
foreach ($data as $v){
   // echo '主进程id--'.posix_getpid().PHP_EOL; //主进程被关闭
    $process= new swoole\Process('my_process');
    //var_dump($process);
    $pid=$process->start(); //开启子进程
    $workers[$pid]=$process;
    $process->write('hello');  //主进程当中写入数据到子进程管道当中

    //echo $process->read().PHP_EOL; //同步阻塞

    //读事件监听,读取内容变成异步读取,在事件触发时才会调用
    swoole_event_add($process->pipe,function ($pipe) use($process){
        echo $process->read().PHP_EOL;
        swoole_event::del($process->pipe);
    });

}



/*//先让子进程执行完毕,然后读取数据
foreach ($workers as $k=>$w){
    echo $w->read().PHP_EOL; //如果说子进程没有往管道当中写数据,主进程读取会阻塞
}*/



//子进程创建成功之后要执行的函数,在闭包函数当中执行的逻辑就是在子进程当中执行
function my_process($worker){
   // echo '子进程id'.$worker->pid.PHP_EOL;
    echo  "来自主进程的消息:". $worker->read().',来自管道'.$worker->pipe.',当前的进程id为'.$worker->pid.PHP_EOL;
    sleep(1);
    $worker->write('主进程你好');
}

//$end_time=microtime(true);
//echo $end_time-$start_time.PHP_EOL;